package com.peas.platform.module.icims.budget.apply.domain.impl;

import cn.hutool.core.lang.Assert;
import cn.hutool.core.util.StrUtil;
import com.peas.platform.framework.mybatisplus.domain.BaseDomainImpl;
import com.peas.platform.framework.mybatisplus.repository.BaseRepository;
import com.peas.platform.module.icims.base.sys.service.SequenceService;
import com.peas.platform.module.icims.budget.apply.adapter.repository.BudgetTwoCollectRepository;
import com.peas.platform.module.icims.budget.apply.adapter.repository.po.BudgetTwoCollectPo;
import com.peas.platform.module.icims.budget.apply.domain.BudgetTwoCollectDomain;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoCollectDto;
import com.peas.platform.module.icims.budget.apply.dto.BudgetTwoCollectRelationDto;
import com.peas.platform.module.icims.budget.apply.service.BudgetTwoCollectRelationService;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.io.Serializable;
import java.util.List;

@Component
public class BudgetTwoCollectDomainImpl extends BaseDomainImpl<BudgetTwoCollectDto, BudgetTwoCollectPo> implements BudgetTwoCollectDomain {

    @Resource
    private BudgetTwoCollectRepository budgetTwoCollectRepository;
    @Resource
    private SequenceService sequenceService;
    @Resource
    private BudgetTwoCollectRelationService budgetTwoCollectRelationService;

    @Override
    public BaseRepository<BudgetTwoCollectPo> getMapper() {
        return budgetTwoCollectRepository;
    }

    @Override
    public Class<? extends BaseRepository<BudgetTwoCollectPo>> getRepositoryClass() {
        return BudgetTwoCollectRepository.class;
    }

    @Transactional
    @Override
    public int save(BudgetTwoCollectDto dto) {
        if (StrUtil.isBlank(dto.getBudgetTwoCollectCode())) {
            String code = sequenceService.genSequence("base", "BudgetTwoCollectDto", "BTC", 4, 1);
            dto.setBudgetTwoCollectCode(code);
        }
        int result = super.save(dto);
        this.checkRepetition("budgetTwoCollectCode", dto.getBudgetTwoCollectCode());
        return result;
    }

    @Transactional
    @Override
    public int updateById(BudgetTwoCollectDto dto) {
        int result = super.updateById(dto);
        this.checkRepetition("budgetTwoCollectCode", dto.getBudgetTwoCollectCode());
        return result;
    }

    @Transactional
    @Override
    public int removeById(Serializable id) {
        List<BudgetTwoCollectRelationDto> bocrds = budgetTwoCollectRelationService.listByField("budgetTwoCollectId", id);
        budgetTwoCollectRelationService.removeBatchByList(bocrds);
        return super.removeById(id);
    }

}
